/**
* MagicRpcException.java 2020/5/8 8:18 下午
* Copyright ©2020 www.bmsoft.com.cn All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package common.exception;

import javax.naming.LimitExceededException;

/**
 * File：MagicRpcException.java<br>
 * Title: <br>
 * Description: <br>
 * Company: www.treefinance.com.cn <br>
 * @author heyouchi
 * Date: 2020/5/8
 */
public class MagicRpcException extends RuntimeException {

  public static final int UNKNOWN_EXCEPTION = 0;
  public static final int NETWORK_EXCEPTION = 1;
  public static final int TIMEOUT_EXCEPTION = 2;
  public static final int BIZ_EXCEPTION = 3;
  public static final int FORBIDDEN_EXCEPTION = 4;
  public static final int SERIALIZATION_EXCEPTION = 5;
  public static final int NO_INVOKER_AVAILABLE_AFTER_FILTER = 6;
  public static final int LIMIT_EXCEEDED_EXCEPTION = 7;
  private static final long serialVersionUID = 7815426752583648734L;
  /**
   * RpcException cannot be extended, use error code for exception type to keep compatibility
   */
  private int code;

  public MagicRpcException() {
    super();
  }

  public MagicRpcException(String message, Throwable cause) {
    super(message, cause);
  }

  public MagicRpcException(String message) {
    super(message);
  }

  public MagicRpcException(Throwable cause) {
    super(cause);
  }

  public MagicRpcException(int code) {
    super();
    this.code = code;
  }

  public MagicRpcException(int code, String message, Throwable cause) {
    super(message, cause);
    this.code = code;
  }

  public MagicRpcException(int code, String message) {
    super(message);
    this.code = code;
  }

  public MagicRpcException(int code, Throwable cause) {
    super(cause);
    this.code = code;
  }

  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public boolean isBiz() {
    return code == BIZ_EXCEPTION;
  }

  public boolean isForbidded() {
    return code == FORBIDDEN_EXCEPTION;
  }

  public boolean isTimeout() {
    return code == TIMEOUT_EXCEPTION;
  }

  public boolean isNetwork() {
    return code == NETWORK_EXCEPTION;
  }

  public boolean isSerialization() {
    return code == SERIALIZATION_EXCEPTION;
  }

  public boolean isNoInvokerAvailableAfterFilter() {
    return code == NO_INVOKER_AVAILABLE_AFTER_FILTER;
  }

  public boolean isLimitExceed() {
    return code == LIMIT_EXCEEDED_EXCEPTION || getCause() instanceof LimitExceededException;
  }
}
